package com.bcx.crm.java.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created by ys on 2018/1/10.
 */
public class MyServletContextListener implements ServletContextListener {
    private static ServletContext servletContext;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        servletContext = sce.getServletContext();
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        System.out.println("我配置的初始化参数context-param的value:"+contextConfigLocation);
        //利用初始化参数找到配置文件机型初始化
        System.out.println("context初始化了");
        System.out.println("假装初始化Spring容器......");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //在销毁之前获取ServletContext
            ServletContext servletContext = sce.getServletContext();
        // 正好刚刚存了一个值进去了，销毁之前拿出来瞅瞅
        Integer count =(Integer) servletContext.getAttribute("count");
        System.out.println("在销毁之前，count的值是:"+count);
    }
}
